package com.edu.edu.day25_homework.activity;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.edu.edu.day25_homework.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/10.
 */
public class WelcomeActivity extends AppCompatActivity{

    private ViewPager mVp;
    private List<ImageView> images;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        mVp = (ViewPager) findViewById(R.id.welcome_vp);

        int[] imagesId = getImagesId();
        images = new ArrayList<>();
        for (int i = 0; i < imagesId.length; i++) {
            ImageView image = new ImageView(this);
            image.setBackgroundResource(imagesId[i]);
            images.add(image);
        }

        mVp.setCurrentItem(0);

        assert mVp != null;
        viewAdapter adapter = new viewAdapter();
        mVp.setAdapter(adapter);
        assert mVp != null;
        mVp.setCurrentItem(100);
//        mVp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//
//            }
//
//            @Override
//            public void onPageSelected(int position) {
//                Log.i("TAG",">>>>>>>>>>>>>imagesId3");
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//
//            }
//        });

        mVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                if(position % images.size() == images.size()-1){
                    finish();
                }
            }
        });

    }

    public void drop(View view){
        finish();
    }

    public int[] getImagesId() {
        return new int[]{R.mipmap.image01,R.mipmap.image05,R.mipmap.image03,R.mipmap.image04,R.mipmap.image02};
    }
    class viewAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(images.get(position % images.size()));
            return images.get(position % images.size());
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(images.get(position % images.size()));
        }
    }

}
